package com.devunion.reader.client.panel;

import com.devunion.reader.client.command.UserManagementCmd;
import com.devunion.reader.client.to.UserManagementTo;
import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.Bindings;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ListLoadConfig;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Aliaksandr Tsimashenka 16.03.2010 : 18:58:49
 */
public class UserManagementPanel extends LayoutContainer {

    private Bindings formBindings;

    @SuppressWarnings("unchecked")
    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setStyleAttribute("margin", "10px");

        ContentPanel cp = new ContentPanel();

        cp.setFrame(true);
        cp.setHeight(370);
        cp.setAutoWidth(true);
        cp.setHeaderVisible(false);
        cp.setLayout(new RowLayout(Style.Orientation.HORIZONTAL));

        final Grid<UserManagementTo> grid = createGrid();
        grid.getSelectionModel().setSelectionMode(Style.SelectionMode.SINGLE);
        grid.getSelectionModel().addListener(Events.SelectionChange,
                new Listener<SelectionChangedEvent<UserManagementTo>>() {
                    public void handleEvent(SelectionChangedEvent<UserManagementTo> be) {
                        if (be.getSelection().size() > 0) {
                            formBindings.bind(be.getSelection().get(0));
                        } else {
                            formBindings.unbind();
                        }
                    }
                });
        cp.add(grid, new RowData(.6, 1));

        formBindings = new Bindings();
        FormPanel panel = createForm(grid);

        //formBindings.setStore(grid.getStore());

        cp.add(panel, new RowData(.4, 1));

        cp.setButtonAlign(Style.HorizontalAlignment.LEFT);
        cp.addButton(new Button("Refresh", new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                grid.getStore().getLoader().load();
            }
        }));

        add(cp);
    }

    private FormPanel createForm(final Grid<UserManagementTo> grid) {
        FormPanel panel = new FormPanel();
        panel.setHeaderVisible(false);

        TextField<String> name = new TextField<String>();
        name.setName(UserManagementTo.USERNAME);
        name.setFieldLabel("Username");
        name.setEnabled(false);
        panel.add(name);
        formBindings.addFieldBinding(new FieldBinding(name, UserManagementTo.USERNAME));

        SimpleComboBox<String> symbol = new SimpleComboBox<String>();
        symbol.add("ADMIN");
        symbol.add("EXPERT");
        symbol.add("NORMAL");
        symbol.setName(UserManagementTo.ROLE);
        symbol.setFieldLabel("Role");
        symbol.setWidth("100%");
        panel.add(symbol);

        SimpleComboBoxFieldBinding symbolFieldBinding = new SimpleComboBoxFieldBinding(symbol, UserManagementTo.ROLE);        
        formBindings.addFieldBinding(symbolFieldBinding);

        panel.addButton(new Button("Commit", new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                grid.getStore().commitChanges();
            }
        }));

        return panel;
    }

    private Grid<UserManagementTo> createGrid() {
        List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

        ColumnConfig column = new ColumnConfig();
        column.setId(UserManagementTo.USERNAME);
        column.setHeader("Username");
        column.setWidth(300);
        configs.add(column);

        column = new ColumnConfig();
        column.setId(UserManagementTo.ROLE);
        column.setHeader("Role");
        column.setWidth(100);
        configs.add(column);

        ListLoader<ListLoadConfig> loader =
                new BaseListLoader<ListLoadConfig, ListLoadResult<UserManagementTo>>(new UserManagementCmd());
        loader.load();
        ListStore<UserManagementTo> store = new ListStore<UserManagementTo>(loader);

        ColumnModel cm = new ColumnModel(configs);

        Grid<UserManagementTo> grid = new Grid<UserManagementTo>(store, cm);
        grid.setBorders(false);
        grid.setLoadMask(true);
        grid.setAutoExpandColumn(UserManagementTo.USERNAME);
        grid.setBorders(true);
        grid.getView().setForceFit(true);

        return grid;
    }
}
